const esbuild = require('esbuild');
const path = require('path');
const fs = require('fs');

const isProduction = process.env.NODE_ENV === 'production';

function copyFonts(fromPath, toPath) {
  // const srcDir = path.join(__dirname, 'node_modules/monaco-editor/esm/vs/base/browser/ui/codicons/codicon');
  // const destDir = path.join(__dirname, 'dist/codicon');
  const srcDir = path.join(__dirname, fromPath);
  const destDir = path.join(__dirname, toPath);
  
  // 确保目标目录存在
  fs.ensureDirSync(destDir);
  // 同步复制整个文件夹
  fs.copySync(srcDir, destDir, {
    overwrite: true // 覆盖已存在的文件
  });
}

// copyFonts('node_modules/monaco-editor/esm/vs/base/browser/ui/codicons/codicon', 'dist/codicon');
// copyFonts('node_modules/monaco-editor/esm/vs', 'dist/vs');
// copyFonts('node_modules/monaco-editor/esm', 'dist/esm');
// copyFonts('node_modules/monaco-editor/min', 'dist/min');

const content = fs.readFileSync('./package.json', 'utf-8')
const json = JSON.parse(content);
// 确保dist目录存在
const distPath = path.join(__dirname, 'dist');
if (!fs.existsSync(distPath)) {
  fs.mkdirSync(distPath);
}

esbuild.build({
  entryPoints: ['src/index.ts'],
  bundle: true,
  outfile: path.join(distPath, 'index.js'),
  platform: 'browser',
  format: 'esm',
  minify: isProduction,
  sourcemap: true,
  loader: {
    '.ttf': 'file' // 关键配置
  },
}).catch(() => process.exit(1));